// --------------------------封装的方法------------------------
// import http from "@/utils/http"
export default {
	gotoLink(url) {
		// console.log(url);
		if (url.indexOf('http') != -1) {
			uni.navigateTo({
				url: `/pages/webview/index?url=${url}`
			})
			return
		}
		let arr = [
			'/pages/index/index',
			'/pages/order/index',
			'/pages/interact/empty',
			'/pages/rank/index',
			'/pages/user/index'
		]
		// tab页
		if ((arr.filter(v => {
				return v == url
			})).length > 0) {
			uni.redirectTo({
				url: url
			})
		} else {
			uni.navigateTo({
				url: url
			})
		}

		// 内嵌页
		// http 开头的
	},
	gotoPage(page, type = 1, islogin = 0) {
		if (islogin == 1) {
			let token = uni.getStorageSync('token');
			if (token) {
				islogin = 0
			}
		}
		if (islogin == 1) {
			uni.navigateTo({
				url: '/pages/login/index'
			})
		} else {
			if (page) {
				switch (type) {
					case 1:
						uni.navigateTo({
							url: page
						});
						break;
					case 2:
						uni.redirectTo({
							url: page
						});
						break;
					case 3:
						uni.switchTab({
							url: page
						})
						break;
				}
			}
		}

	},
	callPhone(params) {
		try {
			uni.makePhoneCall({
				phoneNumber: params.tel, // 电话号码
				success: function() {
					console.log('拨号成功');
				},
				fail: function() {
					console.log('拨号失败');
				}
			});

		} catch (e) {
			//TODO handle the exception
		}
	},
	toPay(paymentData, callback, type) {
		console.log(paymentData);
		uni.requestPayment({
			provider: 'wxpay', //wxpay微信 alipay支付宝
			...paymentData, //订单数据 后台返回的数据  
			success: function(res) {
				console.log(res);
				uni.showToast({
					title: '支付成功',
					icon: 'success'
				})
				callback(res)
			},
			// 参数有问题则抛出错误
			fail: function(err) {
				console.log(err);
				uni.showToast({
					title: '支付失败',
					icon: 'error'
				})
				// callback()
			},
		})
	},
	// 图片拼接
	togetherUrl(arr) {
		if (arr.length > 0) {
			let str = ''
			arr.forEach((v) => {
				str += ',' + v.url
			})
			return str.substring(1)
		} else {
			return ''
		}
	},
	formatDateY_M_D_H_M_S(date,
		bre) {
		if (!date) {
			return ""
		} else {
			let
				br =
				bre ?
				bre : '/';
			let time = new Date(date)
			let year = time.getFullYear()
			let month = time.getMonth() + 1
			let day = time.getDate()
			let hour = time.getHours()
			let min = time.getMinutes()
			let sec = time.getMinutes()
			return year +
				br + (month > 9 ? month : '0' + month) +
				br + (day > 9 ? day : '0' + day) + " " + (hour > 9 ? hour : '0' + hour) +
				":" + (min > 9 ? min : '0' + min) + ":" + (sec > 9 ? sec : '0' + sec)
		}
	},
	formatDateY_M_D(date,
		bre) {
		if (!date) {
			return ""
		} else {
			let
				br =
				bre ?
				bre : '/';
			let time = new Date(date)
			let year = time.getFullYear()
			let month = time.getMonth() + 1
			let day = time.getDate()
			let hour = time.getHours()
			let min = time.getMinutes()
			let sec = time.getMinutes()
			return year +
				br + (month > 9 ? month : '0' + month) +
				br + (day > 9 ? day : '0' + day)
		}
	},
	// 银行卡验证
	verifyBankCardNumber(bankno) {
		bankno = String(bankno).replace(/\s*/g, "")
		var lastNum = bankno.substr(bankno.length - 1, 1); //取出最后一位（与luhn进行比较）

		var first15Num = bankno.substr(0, bankno.length - 1); //前15或18位
		var newArr = new Array();
		for (var i = first15Num.length - 1; i > -1; i--) { //前15或18位倒序存进数组
			newArr.push(first15Num.substr(i, 1));
		}
		var arrJiShu = new Array(); //奇数位*2的积 <9
		var arrJiShu2 = new Array(); //奇数位*2的积 >9

		var arrOuShu = new Array(); //偶数位数组
		for (var j = 0; j < newArr.length; j++) {
			if ((j + 1) % 2 == 1) { //奇数位
				if (parseInt(newArr[j]) * 2 < 9)
					arrJiShu.push(parseInt(newArr[j]) * 2);
				else
					arrJiShu2.push(parseInt(newArr[j]) * 2);
			} else //偶数位
				arrOuShu.push(newArr[j]);
		}

		var jishu_child1 = new Array(); //奇数位*2 >9 的分割之后的数组个位数
		var jishu_child2 = new Array(); //奇数位*2 >9 的分割之后的数组十位数
		for (var h = 0; h < arrJiShu2.length; h++) {
			jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
			jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
		}

		var sumJiShu = 0; //奇数位*2 < 9 的数组之和
		var sumOuShu = 0; //偶数位数组之和
		var sumJiShuChild1 = 0; //奇数位*2 >9 的分割之后的数组个位数之和
		var sumJiShuChild2 = 0; //奇数位*2 >9 的分割之后的数组十位数之和
		var sumTotal = 0;
		for (var m = 0; m < arrJiShu.length; m++) {
			sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
		}

		for (var n = 0; n < arrOuShu.length; n++) {
			sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
		}

		for (var p = 0; p < jishu_child1.length; p++) {
			sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
			sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
		}
		//计算总和
		sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);

		//计算luhn值
		var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
		var luhn = 10 - k;

		if (lastNum == luhn) {
			return true;
		} else {
			return false;
		}
	},
	// 身份证验证
	checkPsidno(value) {
		const psidno = String(value)
		// 1.校验身份证号格式和长度
		const regPsidno =
			/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[X])$)$/
		if (!regPsidno.test(psidno)) {
			return false
		}
		// 2.校验前两位的省份编码是否正确
		const province = {
			11: '北京',
			12: '天津',
			13: '河北',
			14: '山西',
			15: '内蒙古',
			21: '辽宁',
			22: '吉林',
			23: '黑龙江 ',
			31: '上海',
			32: '江苏',
			33: '浙江',
			34: '安徽',
			35: '福建',
			36: '江西',
			37: '山东',
			41: '河南',
			42: '湖北 ',
			43: '湖南',
			44: '广东',
			45: '广西',
			46: '海南',
			50: '重庆',
			51: '四川',
			52: '贵州',
			53: '云南',
			54: '西藏 ',
			61: '陕西',
			62: '甘肃',
			63: '青海',
			64: '宁夏',
			65: '新疆',
			71: '台湾',
			81: '香港',
			82: '澳门',
			91: '国外'
		}
		if (!province[Number(psidno.slice(0, 2))]) {
			return false
		}
		// 3.校验出生日期
		if (psidno.length === 15) {
			// 15位号码 省（2位）市（2位）县（2位）年（2位）月（2位）日（2位）校验码（3位）
			const reg = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/)
			const arrSplit = psidno.match(reg)
			// 15位号码在年份前补 19 或 20
			const year = Number(arrSplit[2].charAt(0)) > 0 ? '19' + arrSplit[2] : '20' + arrSplit[2]
			const month = arrSplit[3]
			const day = arrSplit[4]
			if (!validateBirthday(year, month, day)) {
				return false
			}
		} else if (psidno.length === 18) {
			// 18位号码 省（2位）市（2位）县（2位）年（4位）月（2位）日（2位）校验码（4位）
			const reg = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/)
			const arrSplit = psidno.match(reg)
			const year = arrSplit[2]
			const month = arrSplit[3]
			const day = arrSplit[4]
			if (!validateBirthday(year, month, day)) {
				return false
			}
		} else {
			return false
		}
		// 校验出生日期是否合理
		function validateBirthday(year, month, day) {
			year = Number(year) // 年
			month = Number(month) // 月
			day = Number(day) // 日
			const nowTime = new Date().getTime() // 当前时间戳
			const birthTime = new Date(`${year}-${month}-${day}`).getTime() // 获取出生日期的时间戳
			// 不能是明天出生的吧
			if (birthTime > nowTime) {
				return false
			}
			// 一般人活不到150岁吧
			const nowYear = new Date().getFullYear()
			if ((nowYear - year) > 150) {
				return false
			}
			// 不能是13月出生的吧
			if (month < 1 || month > 12) {
				return false
			}
			// 不能是2月30号、4月31号、5月32号出生的吧
			const date = new Date(year, month, 0) // 获取当月的最后一天
			if (day < 1 || day > date.getDate()) {
				return false
			}
			return true
		}
		// 4.18位号码校验生成的校验码
		if (psidno.length === 18) {
			const Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] // 加权因子
			const parity = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] // 校验码
			let sum = 0
			for (let i = 0; i < 17; i++) {
				sum += Number(psidno.charAt(i)) * Wi[i]
			}
			if (parity[sum % 11] !== psidno[17]) {
				return false
			}
		}
		return true
	}


}